<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Metodología de la Programación II: Fichero Fuente src/LZcoder.cpp</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generado por Doxygen 1.5.5 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="main.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="classes.html"><span>Clases</span></a></li>
      <li class="current"><a href="files.html"><span>Archivos</span></a></li>
    </ul>
  </div>
<h1>src/LZcoder.cpp</h1><a href="LZcoder_8cpp.html">Ir a la documentación de este archivo.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;fstream&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;<a class="code" href="LZcoder_8h.html" title="Interfaz para comprimir un texto con el algoritmo LZ77.">LZcoder.h</a>&gt;</span>
<a name="l00012"></a>00012 
<a name="l00013"></a>00013 <span class="keyword">using namespace </span>std;
<a name="l00014"></a>00014 
<a name="l00015"></a><a class="code" href="classLZcoder.html#c3415941c04cd28bc0ac647ed2a9601b">00015</a> <a class="code" href="classLZcoder.html#c3415941c04cd28bc0ac647ed2a9601b" title="Constructor por defecto  inicializa todo a cero.">LZcoder :: LZcoder</a>()
<a name="l00016"></a>00016 {
<a name="l00017"></a>00017         dist_max=0;
<a name="l00018"></a>00018         indice=0;
<a name="l00019"></a>00019 }
<a name="l00020"></a>00020 
<a name="l00021"></a><a class="code" href="classLZcoder.html#72620a073bc4b515304e39117035df4f">00021</a> <a class="code" href="classLZcoder.html#72620a073bc4b515304e39117035df4f" title="Destructor  libera toda la memoria reservada.">LZcoder :: ~LZcoder</a>()
<a name="l00022"></a>00022 {}
<a name="l00023"></a>00023 
<a name="l00024"></a><a class="code" href="classLZcoder.html#0489c25c1202e211dd56c09b39353d3d">00024</a> <span class="keywordtype">int</span> <a class="code" href="classLZcoder.html#0489c25c1202e211dd56c09b39353d3d">LZcoder :: GetTamano</a>()<span class="keyword">const</span>
<a name="l00025"></a>00025 <span class="keyword"></span>{
<a name="l00026"></a>00026         <span class="keywordflow">return</span> cad.<a class="code" href="classCadena.html#13b89fe1a2bb59a4bbbc9c80b764de16">Size</a>();
<a name="l00027"></a>00027 }
<a name="l00028"></a>00028 
<a name="l00029"></a>00029 <span class="keywordtype">void</span> <a class="code" href="classLZcoder.html#6bd9cb5f2dad8246d13da7e929055fee" title="Lee datos.">LZcoder :: Leer</a>( istream &amp; is )
<a name="l00030"></a>00030 {
<a name="l00031"></a>00031         cad.<a class="code" href="classCadena.html#4991acef3b0775d75041c876df131bda" title="Lee un texto (máximo 65794 caracteres).">Leer</a>(is);
<a name="l00032"></a>00032 }
<a name="l00033"></a>00033 
<a name="l00034"></a><a class="code" href="classLZcoder.html#2170c081cc9e7726fb2aa9215788f522">00034</a> <span class="keywordtype">int</span> <a class="code" href="classLZcoder.html#2170c081cc9e7726fb2aa9215788f522" title="Calcula el siguiente codigo de la cadena buscando la cadena mas larga.">LZcoder :: SiguienteCodigoMejor</a>()
<a name="l00035"></a>00035 {
<a name="l00036"></a>00036     cerr &lt;&lt; <span class="stringliteral">"\r"</span> &lt;&lt; (indice * 100) / <a class="code" href="classLZcoder.html#0489c25c1202e211dd56c09b39353d3d">GetTamano</a>() &lt;&lt; <span class="stringliteral">"%"</span>;
<a name="l00037"></a>00037         <span class="keywordflow">if</span>(indice == (<a class="code" href="classLZcoder.html#0489c25c1202e211dd56c09b39353d3d">GetTamano</a>())) <span class="keywordflow">return</span> 256;
<a name="l00038"></a>00038 
<a name="l00039"></a>00039         <span class="keywordtype">int</span> long_max=0, sig, distancia_maxima=32768,longitud;
<a name="l00040"></a>00040         dist_max=-1;
<a name="l00041"></a>00041         
<a name="l00042"></a>00042         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=indice-1; i&gt;=0 &amp;&amp; (indice-i)&lt;=distancia_maxima; i--)
<a name="l00043"></a>00043         {
<a name="l00044"></a>00044                 longitud=0;
<a name="l00045"></a>00045                 <span class="keywordflow">if</span>(cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(indice) == cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(i))
<a name="l00046"></a>00046                 {
<a name="l00047"></a>00047                         longitud++;
<a name="l00048"></a>00048                         <span class="keywordtype">int</span> distancia=indice - i;
<a name="l00049"></a>00049                         sig=i+1;
<a name="l00050"></a>00050                         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j=indice+1; j&lt;cad.<a class="code" href="classCadena.html#13b89fe1a2bb59a4bbbc9c80b764de16">Size</a>() &amp;&amp; cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(j) == cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(sig) &amp;&amp; longitud&lt;258; j++)
<a name="l00051"></a>00051                         {
<a name="l00052"></a>00052                                 sig++;
<a name="l00053"></a>00053                                 longitud++;
<a name="l00054"></a>00054                         }
<a name="l00055"></a>00055                         <span class="keywordflow">if</span> (longitud &gt; long_max)
<a name="l00056"></a>00056                         {
<a name="l00057"></a>00057                                 long_max=longitud;
<a name="l00058"></a>00058                                 dist_max=distancia;
<a name="l00059"></a>00059                         }
<a name="l00060"></a>00060                 }
<a name="l00061"></a>00061         }
<a name="l00062"></a>00062         indice++;
<a name="l00063"></a>00063         <span class="keywordflow">if</span> (long_max &gt;= 3)
<a name="l00064"></a>00064         {
<a name="l00065"></a>00065                 indice+=(long_max-1);
<a name="l00066"></a>00066                 <span class="keywordflow">return</span> long_max+256;
<a name="l00067"></a>00067         }               
<a name="l00068"></a>00068         <span class="keywordflow">else</span>
<a name="l00069"></a>00069                 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(indice-1);
<a name="l00070"></a>00070 }
<a name="l00071"></a>00071 
<a name="l00072"></a><a class="code" href="classLZcoder.html#54885f3096730720b85eca1b13694106">00072</a> <span class="keywordtype">int</span> <a class="code" href="classLZcoder.html#54885f3096730720b85eca1b13694106" title="Calcula el siguiente codigo de la cadena.">LZcoder :: SiguienteCodigo</a>()
<a name="l00073"></a>00073 {
<a name="l00074"></a>00074     cerr &lt;&lt; <span class="stringliteral">"\r"</span> &lt;&lt; (indice * 100) / <a class="code" href="classLZcoder.html#0489c25c1202e211dd56c09b39353d3d">GetTamano</a>() &lt;&lt; <span class="stringliteral">"%"</span>;
<a name="l00075"></a>00075         <span class="keywordflow">if</span>(indice == (<a class="code" href="classLZcoder.html#0489c25c1202e211dd56c09b39353d3d">GetTamano</a>())) <span class="keywordflow">return</span> 256;
<a name="l00076"></a>00076 
<a name="l00077"></a>00077         <span class="keywordtype">int</span> sig, distancia_maxima=32768,longitud=0;
<a name="l00078"></a>00078         <span class="keywordtype">bool</span> controlador=<span class="keyword">true</span>;
<a name="l00079"></a>00079         dist_max = -1;
<a name="l00080"></a>00080         
<a name="l00081"></a>00081         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=indice-1; controlador==<span class="keyword">true</span> &amp;&amp; i&gt;=0 &amp;&amp; (indice-i)&lt;=distancia_maxima; i--)
<a name="l00082"></a>00082         {
<a name="l00083"></a>00083                 longitud=0;
<a name="l00084"></a>00084                 <span class="keywordflow">if</span>(cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(indice) == cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(i))
<a name="l00085"></a>00085                 {
<a name="l00086"></a>00086                         longitud++;
<a name="l00087"></a>00087                         <span class="keywordtype">int</span> distancia = indice - i;
<a name="l00088"></a>00088                         sig=i+1;
<a name="l00089"></a>00089                         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j=indice+1; j&lt;cad.<a class="code" href="classCadena.html#13b89fe1a2bb59a4bbbc9c80b764de16">Size</a>() &amp;&amp; cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(j) == cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(sig) &amp;&amp; longitud&lt;258; j++)
<a name="l00090"></a>00090                         {
<a name="l00091"></a>00091                                 sig++;
<a name="l00092"></a>00092                                 longitud++;
<a name="l00093"></a>00093                         }
<a name="l00094"></a>00094                         <span class="keywordflow">if</span> (longitud &gt;= 3 )
<a name="l00095"></a>00095                         {
<a name="l00096"></a>00096                                 controlador=<span class="keyword">false</span>;
<a name="l00097"></a>00097                                 dist_max = distancia;
<a name="l00098"></a>00098                         }
<a name="l00099"></a>00099                 }
<a name="l00100"></a>00100         }
<a name="l00101"></a>00101         <span class="keywordflow">if</span> (longitud &gt;= 3)
<a name="l00102"></a>00102         {
<a name="l00103"></a>00103                 indice+=(longitud);
<a name="l00104"></a>00104                 <span class="keywordflow">return</span> longitud+256;
<a name="l00105"></a>00105         }               
<a name="l00106"></a>00106         <span class="keywordflow">else</span>{
<a name="l00107"></a>00107                 indice++;
<a name="l00108"></a>00108                 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)cad.<a class="code" href="classCadena.html#2cb0d0d665519bfdc2fbe1eb7abe0d0a">GetCaracter</a>(indice-1);
<a name="l00109"></a>00109         }
<a name="l00110"></a>00110 }
<a name="l00111"></a>00111 
<a name="l00112"></a><a class="code" href="classLZcoder.html#acb2e0ee6ae1623554b60b9712c601b2">00112</a> <span class="keywordtype">int</span> <a class="code" href="classLZcoder.html#acb2e0ee6ae1623554b60b9712c601b2">LZcoder :: GetDistancia</a> ()<span class="keyword"> const</span>
<a name="l00113"></a>00113 <span class="keyword"></span>{
<a name="l00114"></a>00114         <span class="keywordflow">return</span> dist_max;
<a name="l00115"></a>00115 }
<a name="l00116"></a>00116 
<a name="l00117"></a><a class="code" href="classLZcoder.html#f2fc72da4d2546cdb4e5f7dbd11a05c7">00117</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classLZcoder.html#f2fc72da4d2546cdb4e5f7dbd11a05c7" title="Calculo del crc32.">LZcoder :: GetParidad</a> ()<span class="keyword"> const</span>
<a name="l00118"></a>00118 <span class="keyword"></span>{
<a name="l00119"></a>00119         <span class="keywordflow">return</span> cad.<a class="code" href="classCadena.html#ab67ba5cd03cc09a6231fb40428fcd91" title="Calculo del crc32.">GetParidad</a> ();
<a name="l00120"></a>00120 }
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Generado el Thu Jun 10 12:36:12 2010 para Metodología de la Programación II por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.5 </small></address>
</body>
</html>
